var __extends = this.__extends || function (d, b) {
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var Drawable = (function () {
    function Drawable(ctx, x, y) {
        this.active = false;
        this.ctx = ctx;
        this.x = x;
        this.y = y;
    }
    Drawable.prototype.Draw = function () {
    };
    Drawable.prototype.Click = function (x, y) {
        return false;
    };
    return Drawable;
})();
; ;
var Corner = (function (_super) {
    __extends(Corner, _super);
    function Corner(ctx, x, y, radius) {
        _super.call(this, ctx, x, y);
        this.radius = radius;
        this.squeres = new Array();
    }
    Corner.prototype.Draw = function () {
        var startPoint = (Math.PI / 180) * 0;
        var endPoint = (Math.PI / 180) * 360;
        this.ctx.beginPath();
        this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
        this.ctx.fillStyle = this.active ? "rgb(255, 0, 0)" : "rgb(0, 255, 0)";
        this.ctx.fill();
        this.ctx.closePath();
    };
    Corner.prototype.AddSquere = function (squere) {
        this.squeres.push(squere);
    };
    Corner.prototype.Click = function (x, y) {
        if(this.active) {
            return false;
        }
        if(Math.abs(x - this.x) <= (this.radius * 2) && Math.abs(y - this.y) <= (this.radius * 2)) {
            this.active = true;
            this.squeres.forEach(function (corner) {
                corner.CornerChecked();
            });
            return true;
        }
        return false;
    };
    return Corner;
})(Drawable);
var Squere = (function (_super) {
    __extends(Squere, _super);
    function Squere(ctx, x, y, width) {
        _super.call(this, ctx, x, y);
        this.cornersChecked = 0;
        this.width = width;
    }
    Squere.prototype.CornerChecked = function () {
        this.cornersChecked++;
        if(this.cornersChecked == 4) {
            createsoundbite();
            this.active = true;
            this.Draw();
        }
    };
    Squere.prototype.Draw = function () {
        var steps = 30, i = 0, _this = this;
        var interval = setInterval(function () {
            _this.ctx.fillStyle = 'rgba(255,0,0,' + i / steps + ')';
            _this.ctx.fillRect(_this.x, _this.y, _this.width, _this.width);
            i++;
            if(i === steps) {
                clearInterval(interval);
            }
        }, 30);
    };
    return Squere;
})(Drawable);
var Game = (function () {
    function Game(canvas) {
        this.ctx = (canvas).getContext('2d');
        this.LeftOffset = findPos(canvas)[0];
        this.TopOffset = findPos(canvas)[1];
        this.Width = canvas.clientWidth;
    }
    Game.prototype.Init = function (n) {
        this.ctx.clearRect(0, 0, this.ctx.canvas.clientWidth, this.ctx.canvas.clientWidth);
        this.corners = new Array();
        var distance = this.Width / (n + 1);
        var radius = distance / 6;
        for(var i = 0; i < n; i++) {
            this.corners[i] = new Array();
            for(var j = 0; j < n; j++) {
                this.corners[i][j] = new Corner(this.ctx, i * distance + distance, j * distance + distance, radius);
                if(i > 0 && j > 0) {
                    var sq = new Squere(this.ctx, (i - 1) * distance + distance, (j - 1) * distance + distance, distance);
                    this.corners[i - 1][j - 1].AddSquere(sq);
                    this.corners[i][j - 1].AddSquere(sq);
                    this.corners[i - 1][j].AddSquere(sq);
                    this.corners[i][j].AddSquere(sq);
                }
            }
        }
    };
    Game.prototype.Draw = function () {
        this.corners.forEach(function (row) {
            row.forEach(function (corner) {
                corner.Draw();
            });
        });
    };
    Game.prototype.Click = function (e) {
        var click_X = e.pageX - this.LeftOffset;
        var click_Y = e.pageY - this.TopOffset;
        this.corners.forEach(function (row) {
            row.forEach(function (corner) {
                corner.Click(click_X, click_Y);
            });
        });
        this.Draw();
    };
    return Game;
})();
function findPos(obj) {
    var curleft = 0;
    var curtop = 0;
    if(obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }while(obj = obj.offsetParent)
        return [
            curleft, 
            curtop
        ];
    }
}
var canvas = document.getElementById('gameCanvas');
var game = new Game(canvas);
game.Init(10);
game.Draw();
canvas.addEventListener("click", function (e) {
    game.Click(e);
}, true);
var bgmusic = new Audio();
bgmusic.src = "/Content/theme-x-stephen_burns.mp3";
bgmusic.loop = true;
bgmusic.play();
function createsoundbite() {
    var html5audio = new Audio();
    html5audio.setAttribute('src', '/Content/laser.mp3');
    html5audio.play();
    delete html5audio;
}
function ToggleMusic() {
    if(bgmusic.paused == false) {
        bgmusic.pause();
        document.getElementById('sound_icon').style.background = 'url(/Content/sound_off.png)';
    } else {
        bgmusic.play();
        document.getElementById('sound_icon').style.background = 'url(/Content/sound_on.png)';
    }
}
